package com.zkingsoft.actions.superaction;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.model.Company;
import com.zkingsoft.model.Function;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.CompanyService;
import com.zkingsoft.services.FunctionService;

/**
 * @description 公司管理action
 * @author 姜友瑶
 * @email 935090232@qq.com
 * @date 2016-06-26
 */
@Controller
@RequestMapping(value = "/super/company")
public class CompanyController extends BaseController {

	@Resource
	private CompanyService currentService;
	@Resource
	private FunctionService functionService;

	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, Company company, PaginationVO pageVo) {
		return showList(currentService, company, pageVo);
	}

	/**
	 * 
	 * @Description: 新增或修改公司
	 * @author:姜友瑶
	 * @param company
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Company company) {
		// 验证去重
		if (currentService.checkIsRepeat(company)) {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "添加失败，公司已经存在！");
		}
		if (company.getId() != null) {
			return modify(currentService, company, "公司");
		} else {
			company.setRegisterTime(new Date());
			return add(currentService, company, "公司");
		}
	}

	/**
	 * 
	 * @Description: 删除公司
	 * @author:姜友瑶
	 * @param keys
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	/**
	 * 
	 * @Description: 进入编辑页面
	 * @author:姜友瑶
	 * @param id
	 * @param request
	 * @return 返回类型 String
	 * @date 2016年8月31日
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id, HttpServletRequest request) {
		Company company;
		if (id != null) {
			company = currentService.findById(id);
			request.setAttribute("obj", company);
		}
		List<Function> list = functionService.findByModel(null);
		request.setAttribute("list", list);
		return "super/company-form";
	}

	/**
	 * 
	 * @Description: 查询所有公司
	 * @author:姜友瑶
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		return findByModel(currentService, null);
	}
}